/*
 * @Description: 
 * @Author: 张凌瑞
 * @Date: 2021-02-25 15:54:42
 * @LastEditTime: 2021-02-26 20:39:02
 * @LastEditors: 张凌瑞
 */

// import { Hists } from "../../interface/car.interface";

// interface 
import { GETDATA ,TAB} from "../types/car.types";
import { IcarState ,List } from "../../interface/car.interface";


let init:IcarState = {
    data: {},//所有的数据
    id: 2573,
    list: [],//
    yearList: [],//年份
    years: [],//年
    tab:'全部',
}

function carReducer(state = init, action: any) {
    switch (action.type) {
        case GETDATA:
           let yearList:any = {}
            action.data.list.forEach((item: List) => {
                let year: any = item.market_attribute.year;
                if (!state.yearList.length) {
                    if (yearList[year]) {
                        yearList[year].push(item)
                        } else {
                            yearList[year] = [];
                            yearList[year].push(item)
                        }
                }
               
            })
            state.years = Object.keys(yearList);
            return { ...state, data: action.data ,list:action.data.list,yearList };
        case TAB:
            state.tab = action.i;
            let list = []
            
            if (action.i ==='全部' ) {
                list = state.data.list as [];
            } else {
                list = state.yearList[action.i];
                
            }
            // state.list = state.list.filter((item: any) => {
            //     return item.market_attribute.year == action.i;
            // })
            // console.log(state.list)
            return { ...state,list };
        default:
            return state;
    }
    
}

export default carReducer;